package com.example.tasks;

import java.time.Instant;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

/**
 * 异步方法调用测试
 * 
 * @ClassName: AsyncTask
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author liuwj3
 * @date 2018年12月24日 下午10:49:20
 *
 */
@Component
public class AsyncTask {

	@Async
	public Future<Boolean> doTask1() throws Exception {
		long start = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		TimeUnit.SECONDS.sleep(1);
		long end = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		
		System.out.println("任务1耗时：" + (end - start) + "毫秒");
		
		return new AsyncResult<Boolean>(Boolean.TRUE);
	}
	
	@Async
	public Future<Boolean> doTask2() throws Exception {
		long start = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		TimeUnit.MILLISECONDS.sleep(700);
		long end = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		
		System.out.println("任务2耗时：" + (end - start) + "毫秒");
		
		return new AsyncResult<Boolean>(Boolean.TRUE);
	}
	
	@Async
	public Future<Boolean> doTask3() throws Exception {
		long start = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		TimeUnit.MILLISECONDS.sleep(600);
		long end = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		
		System.out.println("任务3耗时：" + (end - start) + "毫秒");
		
		return new AsyncResult<Boolean>(Boolean.TRUE);
	}

}
